package com.yxw.live_vod_boot.utils;

import java.net.NetworkInterface;
import java.net.InterfaceAddress;
import java.net.InetAddress;
import java.util.Enumeration;
import java.util.ArrayList;
import com.yxw.yxnet_cd_center.common.utils.PasswordUtil;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Collections;
import com.yxw.live_vod_boot.vo.MacVO;
/**
 *
 * @author Administrator
 */
public class SysUtil {
    private static volatile String cachedIHostUniqueCode = null;    
    public static List<MacVO> getMacList() throws Exception
    {
        Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
        StringBuilder sb = new StringBuilder();
        ArrayList<MacVO> tmpMacList=new ArrayList<>();
        while(en.hasMoreElements())
        {
            NetworkInterface iface = en.nextElement();
            if(iface.getMTU() > 9)
            {
                List<InterfaceAddress> addrs = iface.getInterfaceAddresses();
                for(InterfaceAddress addr : addrs) 
                {

                    InetAddress ip = addr.getAddress();
                    NetworkInterface network = NetworkInterface.getByInetAddress(ip);
                    if(network==null)
                    {
                        continue;
                    }
                    byte[] mac = network.getHardwareAddress();
                    if(mac==null)
                    {
                        continue;
                    }
                    sb.delete( 0, sb.length() );
                    for (int i = 0; i < mac.length; i++) 
                    {
                        sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
                    }
                    MacVO oMacVO = new MacVO();
                    oMacVO.setHostMac(sb.toString().replaceAll("-", "").replaceAll(":", ""));
                    tmpMacList.add(oMacVO);
                }            
            }
        }
        if(tmpMacList.size()<=0)
        {
            return tmpMacList;
        }
        
        List<MacVO> unique = tmpMacList.stream().distinct().collect(Collectors.toList());
        Collections.sort(unique);
        return unique;
    }    
    public static String getHostUniqueCode()
    {
        if (null != cachedIHostUniqueCode) {
            return cachedIHostUniqueCode;
        }
        List<MacVO> unique;
        try 
        {
            unique = getMacList();
            //System.out.println(SysUtil.getMacList().toString());
        } 
        catch (Exception e) 
        {
            unique = null;
            System.out.println(e.getMessage());           
        }
        String matter = "";
        if(null != unique)
        {
            for(MacVO tmp : unique)
            {
                matter = matter + tmp.getHostMac().trim().toLowerCase();
            }            
        }
        else
        {
            matter = IpUtil.getIp();
        }
        //System.out.println(matter);
        matter = PasswordUtil.md5Hex(matter);
        cachedIHostUniqueCode = matter;
        return matter;
    }
}
